#coding=utf-8
# !/usr/bin/python

import matplotlib
import numpy as np
matplotlib.use("Qt5Agg")
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

from PySide6 import QtWidgets, QtCore
from matplotlib.lines import Line2D
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

class c_Mplplot(FigureCanvas):
    def __init__(self, parent=None, width=5, height=3, dpi=100):
        # normalized for 中文显示和负号
        plt.rcParams['font.sans-serif'] = ['SimHei']
        plt.rcParams['axes.unicode_minus'] = False
        plt.tight_layout(rect=[0.1, 0.2, 0.3, 0.4])
        #fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9)
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        super(c_Mplplot, self).__init__(self.fig)
        self.setParent(parent)
        self.axes = self.fig.add_subplot(111) # 111 表示 1 行 1 列，第一张曲线图
        self.fig.subplots_adjust(left=0.03, bottom=0.03, right=0.99, top=0.99)
        
        #FigureCanvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    
    def add_line(self, xlim, x_data, y_data):
        self.line1 = Line2D(x_data, y_data)  # 绘制 2D 折线图
        self.line2 = Line2D(x_data, y_data)
        self.line3 = Line2D(x_data, y_data)
        self.line4 = Line2D(x_data, y_data)
        self.line5 = Line2D(x_data, y_data)
        self.line6 = Line2D(x_data, y_data)
        
        # ------------------ 调整折线图基本样式 ---------------------#
        # self.line.set_ls('--')  # 设置连线
        # self.line.set_marker('*') # 设置每个点
        self.line1.set_color('red')  # 设置线条颜色
        self.line2.set_color('blue')  # 设置线条颜色
        self.line3.set_color('m')  # 洋红
        self.line4.set_color('green')  # 绿色
        self.line5.set_color('y')  # 黄色
        self.line6.set_color('k')  # 黑色
   
        #self.line5.set_linestyle('--')
        #self.line6.set_linestyle('--')
        
        # self.axes.grid(True)  # 添加网格
        
        self.axes.set_title('')  # 设置标题
        # 设置 xy 轴最大最小值 , 找到 x_data, y_data 最大最小值
        self.axes.set_xlim( 0, xlim )
        self.axes.set_ylim(-200, 200)  # y 轴稍微多一点，会好看一点
        
        self.axes.set_xlabel('')  # 设置坐标名称
        self.axes.set_ylabel('')
        # 在曲线下方填充颜色
        # self.ax.fill_between(x_data, y_data, color='g', alpha=0.1)
        # self.ax.legend([self.line], ['sinx'])  # 添加图例
        # ------------------------------------------------------#
        self.axes.add_line(self.line1)
        self.axes.add_line(self.line2)
        self.axes.add_line(self.line3)
        self.axes.add_line(self.line4)
        self.axes.add_line(self.line5)
        self.axes.add_line(self.line6)
        

